//
//  CityTableViewCell.m
//  WWTQ
//
//  Created by JP on 2018/10/27.
//  Copyright © 2018年 Jqert. All rights reserved.
//

#import "CityTableViewCell.h"

@interface CityTableViewCell ()
@property (nonatomic, strong) UIImageView *weather;//天气
@property (nonatomic, strong) UIImageView *locationCity;
@property (nonatomic, strong) UILabel *cityName;
@property (nonatomic, strong) UILabel *temperature;
@end

@implementation CityTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    [self.contentView addSubview:self.weather];
    [self.contentView addSubview:self.locationCity];
    [self.contentView addSubview:self.cityName];
    [self.contentView addSubview:self.temperature];
    
    [self setUpLayoutWithMasonry];
}

- (void)setDict:(NSDictionary *)dict {
    _dict = dict;
    _cityName.text = dict[@"name"];
    NSString *high = [dict[@"high"] substringFromIndex:3];
    NSString *low = [dict[@"low"] substringFromIndex:3];
    NSString *lowD = [low substringToIndex:low.length - 1];
    
    _temperature.text = [NSString stringWithFormat:@"%@ ~ %@", lowD, high];
    NSString *imageName = [UIImageView addImageName:dict[@"type"]];
    _weather.image = [UIImage imageNamed:imageName];
    //选中的城市
    if ([dict[@"name"] isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"locationCityName"]]) {
        _locationCity.hidden = NO;
    }
}

- (void)setUpLayoutWithMasonry {
    [_weather mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(40, 30));
    }];
    [_cityName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.weather.mas_right).offset(10);
        make.centerY.mas_equalTo(self.weather);
//        make.size.mas_equalTo(CGSizeMake(150, 40));
        make.height.mas_equalTo(40);
    }];
    [_locationCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cityName.mas_right).offset(2);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    [_temperature mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-10);
        make.centerY.mas_equalTo(self.weather);
        make.size.mas_equalTo(CGSizeMake(200, 40));
    }];
}

- (UIImageView *)weather {
    if (!_weather) {
        _weather = [[UIImageView alloc] init];
        _weather.clipsToBounds = YES;
        _weather.contentMode = UIViewContentModeScaleAspectFit;
        _weather.image = [UIImage imageNamed:@"wushuju"];
    }
    return _weather;
}

- (UIImageView *)locationCity {
    if (!_locationCity) {
        _locationCity = [[UIImageView alloc] init];
        _locationCity.clipsToBounds = YES;
        _locationCity.contentMode = UIViewContentModeScaleAspectFit;
        _locationCity.image = [UIImage imageNamed:@"dingweiCity"];
        _locationCity.hidden = YES;
    }
    return _locationCity;
}


- (UILabel *)cityName {
    if (!_cityName) {
        _cityName = [[UILabel alloc] init];
        _cityName.textColor = ZYTBlack1Color;
        _cityName.font = [UIFont systemFontOfSize:18.0];
//        _cityName.text = @"北京市";
    }
    return _cityName;
}

- (UILabel *)temperature {
    if (!_temperature) {
        _temperature = [[UILabel alloc] init];
        _temperature.textAlignment = 2;
//        _temperature.textColor = [UIColor blackColor];
        _temperature.textColor = ZYTBlack1Color;
        _temperature.font = [UIFont fontWithName:@"Verdana" size:16.0];
        _temperature.text = @"18 ~ 23°C";
    }
    return _temperature;
}


@end
